home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / dynload_win.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  2.9 KB  |  122 lines

  1. /* Support for dynamic loading of extension modules */
  2.  
  3. #include <windows.h>
  4. #include <direct.h>
  5.  
  6. #include "Python.h"
  7. #include "importdl.h"
  8.  
  9. const struct filedescr _PyImport_DynLoadFiletab[] = {
  10. #ifdef _DEBUG
  11.     {"_d.pyd", "rb", C_EXTENSION},
  12.     {"_d.dll", "rb", C_EXTENSION},
  13. #else
  14.     {".pyd", "rb", C_EXTENSION},
  15.     {".dll", "rb", C_EXTENSION},
  16. #endif
  17.     {0, 0}
  18. };
  19.  
  20.  
  21. dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
  22.                     const char *pathname, FILE *fp)
  23. {
  24.     dl_funcptr p;
  25.     char funcname[258];
  26.  
  27.     sprintf(funcname, "init%.200s", shortname);
  28.  
  29. #ifdef MS_WIN32
  30.     {
  31.         HINSTANCE hDLL;
  32.         char pathbuf[260];
  33.         if (strchr(pathname, '\\') == NULL &&
  34.             strchr(pathname, '/') == NULL)
  35.         {
  36.             /* Prefix bare filename with ".\" */
  37.             char *p = pathbuf;
  38.             *p = '\0';
  39.             _getcwd(pathbuf, sizeof pathbuf);
  40.             if (*p != '\0' && p[1] == ':')
  41.                 p += 2;
  42.             sprintf(p, ".\\%-.255s", pathname);
  43.             pathname = pathbuf;
  44.         }
  45.         /* Look for dependent DLLs in directory of pathname first */
  46.         /* XXX This call doesn't exist in Windows CE */
  47.         hDLL = LoadLibraryEx(pathname, NULL,
  48.                      LOAD_WITH_ALTERED_SEARCH_PATH);
  49.         if (hDLL==NULL){
  50.             char errBuf[256];
  51.             unsigned int errorCode;
  52.  
  53.             /* Get an error string from Win32 error code */
  54.             char theInfo[256]; /* Pointer to error text
  55.                           from system */
  56.             int theLength; /* Length of error text */
  57.  
  58.             errorCode = GetLastError();
  59.  
  60.             theLength = FormatMessage(
  61.                 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
  62.                 NULL, /* message source */
  63.                 errorCode, /* the message (error) ID */
  64.                 0, /* default language environment */
  65.                 (LPTSTR) theInfo, /* the buffer */
  66.                 sizeof(theInfo), /* the buffer size */
  67.                 NULL); /* no additional format args. */
  68.  
  69.             /* Problem: could not get the error message.
  70.                This should not happen if called correctly. */
  71.             if (theLength == 0) {
  72.                 sprintf(errBuf,
  73.                     "DLL load failed with error code %d",
  74.                     errorCode);
  75.             } else {
  76.                 int len;
  77.                 /* For some reason a \r\n
  78.                    is appended to the text */
  79.                 if (theLength >= 2 &&
  80.                     theInfo[theLength-2] == '\r' &&
  81.                     theInfo[theLength-1] == '\n') {
  82.                     theLength -= 2;
  83.                     theInfo[theLength] = '\0';
  84.                 }
  85.                 strcpy(errBuf, "DLL load failed: ");
  86.                 len = strlen(errBuf);
  87.                 strncpy(errBuf+len, theInfo,
  88.                     sizeof(errBuf)-len);
  89.                 errBuf[sizeof(errBuf)-1] = '\0';
  90.             }
  91.             PyErr_SetString(PyExc_ImportError, errBuf);
  92.         return NULL;
  93.         }
  94.         p = GetProcAddress(hDLL, funcname);
  95.     }
  96. #endif /* MS_WIN32 */
  97. #ifdef MS_WIN16
  98.     {
  99.         HINSTANCE hDLL;
  100.         char pathbuf[16];
  101.         if (strchr(pathname, '\\') == NULL &&
  102.             strchr(pathname, '/') == NULL)
  103.         {
  104.             /* Prefix bare filename with ".\" */
  105.             sprintf(pathbuf, ".\\%-.13s", pathname);
  106.             pathname = pathbuf;
  107.         }
  108.         hDLL = LoadLibrary(pathname);
  109.         if (hDLL < HINSTANCE_ERROR){
  110.             char errBuf[256];
  111.             sprintf(errBuf,
  112.                 "DLL load failed with error code %d", hDLL);
  113.             PyErr_SetString(PyExc_ImportError, errBuf);
  114.             return NULL;
  115.         }
  116.         p = GetProcAddress(hDLL, funcname);
  117.     }
  118. #endif /* MS_WIN16 */
  119.  
  120.     return p;
  121. }
  122.